

package cn.shengchao.examstar.me.play.domain;

import lombok.Data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class VideoAnalysis {
    public static Result analyze(List<Point> records) {
        int actualWatchTime = 0; // 实际观看时长（秒）
        List<Interval> intervals = new ArrayList<>(); // 用于记录有效播放区间

        int threshold = 200; // 快进阈值（10分钟）

        for (int i = 1; i < records.size(); i++) {
            Point prev = records.get(i - 1); // 上一条记录
            Point current = records.get(i); // 当前记录

            int prevProgress = (int)prev.getPoint(); // 上一次播放进度
            int currProgress = (int)current.getPoint(); // 当前播放进度
            if (currProgress > prevProgress) {
                if (currProgress - prevProgress > threshold) {
                    // 快进超过阈值：跳过
                    continue;
                }
                // 累计实际观看时长（每条记录固定间隔10秒）
                actualWatchTime += 10;

                // 记录有效播放区间
                intervals.add(new Interval(prevProgress, currProgress));
            }
        }

        // 合并重叠或连续的区间，计算实际进度
        intervals = mergeIntervals(intervals);
        long totalProgress = totalProgress(intervals);
        return new Result(actualWatchTime, totalProgress, intervals);
    }

    public static long totalProgress(List<Interval> intervals) {
        int totalProgress = 0;
        for (Interval interval : intervals) {
            totalProgress += interval.end - interval.start;
        }
        return totalProgress;
    }

    // 合并区间
    public static List<Interval> mergeIntervals(List<Interval> intervals) {
        if (intervals.isEmpty())
            return Collections.emptyList();
        if (intervals.size() == 1) {
            return intervals;
        }
        // 按区间起点排序
        intervals.sort(Comparator.comparingInt(a -> a.start));
        List<Interval> merged = new ArrayList<>();

        Interval current = intervals.getFirst();
        for (int i = 1; i < intervals.size(); i++) {
            Interval next = intervals.get(i);
            if (current.end >= next.start) {
                // 有重叠或连续，合并区间
                current.end = Math.max(current.end, next.end);
            } else {
                // 无重叠，保存当前区间并更新
                merged.add(current);
                current = next;
            }
        }
        merged.add(current); // 保存最后一个区间
        return merged;
    }

    @Data
    // 结果类，存储实际观看时长和进度
    public static class Result {
        int actualWatchTime; // 实际观看时长
        long totalProgress;   // 实际观看进度
        List<Interval> intervals;

        public Result(int actualWatchTime, long totalProgress, List<Interval> intervals) {
            this.actualWatchTime = actualWatchTime;
            this.totalProgress = totalProgress;
            this.intervals = intervals;
        }
    }

    @Data
    // 区间类
    public static class Interval {
        int start;
        int end;

        public Interval(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }
}
